package java_thinking.unit_21.Exercise.AQS;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2019-07-08 22:49
 */

public class ReenterLockTest implements Runnable{
    public static ReentrantLock lock = new ReentrantLock(false);
    public static int state = 0;
    @Override
    public void run() {

            for (int i = 0; i < 50; i++) {
                lock.lock();
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(3000);
                    state++;
                    System.out.println(lock.getHoldCount());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                    lock.unlock();
                }
            }

    }

    public static void main(String[] args) throws InterruptedException {
        ReenterLockTest lockTest = new ReenterLockTest();
        Thread t1 = new Thread(lockTest);
        Thread t2 = new Thread(lockTest);
        t1.start();t2.start();
        t1.join();t2.join();
        System.out.println(state);
    }
}


class ResourceByCondition1 {
    private String name;
    private int count = 1;
    /**
     * 当flag为true时，代表烤鸭生成完毕
     */
    private boolean flag = false;
    Lock lock = new ReentrantLock();
    Lock lock1 = new ReentrantLock();
    /**
     * 通过已有的锁获取两组监视器，一组监视生产者，一组监视消费者
     */
    Condition producer_con = lock.newCondition();
    Condition consumer_con = lock.newCondition();

    public void product(String name) {
        lock.lock();
        try {
            flag = false;
            System.out.println("lockThread :"+ Thread.currentThread().getName());
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("unlock");
            lock.unlock();
        }

    }
    public void consumer() {
//        lock.lock();
        lock.lock();

        try {
            flag = true;
            System.out.println("lockThread :"+ Thread.currentThread().getName());
            System.out.println("consumer");
        } finally {
            lock.unlock();
        }

    }
    public static void main(String[] args) throws InterruptedException {
        ResourceByCondition1 condition = new ResourceByCondition1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                condition.product("inter");

            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                while (!condition.flag) {
//                    condition.product("con");
                    System.out.println("pre");
                    condition.consumer();
                }
            }
        }).start();
    }

}